﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

namespace SoftwareEngineering_Project
{
    public class Employee
    {
        int EmployeeID;
        public string FName;
        public string LName;
        public string Email;
        public SqlDataReader EmployeeReader;
        //Create a new employee instance
        public Employee(string FName,string LName, string Email)
        {
            this.FName = FName;
            this.LName = LName;
            this.Email = Email;
        }
        public Employee() { }
        public Boolean AddtoDataBase()
        {
            string sqlcommand;
            sqlcommand = "INSERT INTO Employee VALUES('" + FName + "','" + LName + "','" + Email + "');";
            DBConnection DB = new DBConnection();
            //DB.cnTransact.Open();
            // Myusers.DataSource = DB.ExecuteCommandquery(sqlcommand);
            if (DB.ExecuteCommandNonquery(sqlcommand))
            {
                (new Logs()).CreateLog(DB, "7", "");
                return true;
            }
            else
            {
                return false;
            }

            ////DB.cnTransact.Close();
            
            return false;
        }
        public Boolean CheckifuserExists(string email)
        {
            string sqlcommand;
            sqlcommand = "select * from Employee where Email='"+email +"'";
            DBConnection DB = new DBConnection();
            ////DB.cnTransact.Open();
            // Myusers.DataSource = DB.ExecuteCommandquery(sqlcommand);
            if (DB.ExecuteCommandquery(sqlcommand).HasRows)
            {
                
                ////DB.cnTransact.Close();
                return true;
            }
            else
            {
                
                ////DB.cnTransact.Close();
                return false;
            }

            

           
        }
        public Int32 getEmployeeID()
        {
            string sqlcommand;
            sqlcommand = "SELECT IDENT_CURRENT('Employee');";
            DBConnection DB = new DBConnection();
            ////DB.cnTransact.Open();
            // Myusers.DataSource = DB.ExecuteCommandquery(sqlcommand);
            Int32 myID = DB.GetlastID(sqlcommand);
            //EmployeeReader = DB.ExecuteCommandquery(sqlcommand); 
            ////DB.cnTransact.Close();
            return myID;

        }
        public void getEmployeeByID(string EmployeeID)
        {
            SqlDataReader myReader;
            string query = "SELECT FName, LName, Email FROM Employee WHERE (Employee_ID = '" + EmployeeID + "');";
            DBConnection DB = new DBConnection();
            //DB.cnTransact.Open();
            myReader = DB.ExecuteCommandquery(query);
            if (myReader.HasRows)
            {
                myReader.Read();
                this.FName = (string)myReader["FName"];
                this.LName = (string)myReader["LName"];
                this.Email = (string)myReader["Email"];
            }
            //DB.cnTransact.Close();
        }
    }
}